#!/usr/bin/python
#coding:utf-8
import requests
import json
import random
from datetime import datetime, timedelta

HIKE_ID = '20201027162945282'

def get_date(date_str):
    if date_str is None or len(date_str) == 0:
        date = datetime.now()
    else:
        date = datetime.strptime(date_str, "%Y-%m-%d")
    return date

# def get_hike_id():
def before_date(date=datetime.now()):
    return date+timedelta(days=-1)

# 上传步数
def update_step(task_id, date="2023-01-14", job_num='041538'):
    url = 'https://mcrm.citics.com/union/mobile/life/sport/hike/v2/updateDailyLog'
    proxies = {'https': 'http://127.0.0.1:8888'}
    header = {'Content-Type': 'application/x-www-form-urlencoded',
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'}
    opt['EMPE_NO'] = job_num
    opt['HIKE_ID'] = HIKE_ID
    opt['TASK_ID'] = task_id
    opt['STEP'] = random.randint(6001,6999)
    opt['HIKE_DATE'] = date
    opt['OPEN_ID'] = 'oWMkR5X6Z7AjPR8-46UbPAMTM8Oc'
    html = requests.post(url, data=opt, headers=header)
    #html = requests.post(url, data=opt, headers=header,proxies=proxies, verify=False)
    if html.status_code == 200:
        json_text = json.loads(html.text)
        print('上传步数：', json_text['result'])
        
# 获取任务列表
def get_last_task_id(token, job_num='041538'):
    url = 'https://mcrm.citics.com/union/mobile/life/sport/hike/v2/queryTaskList?EMPE_NO=%s&HIKE_ID=%s&PAGE_NUM=0&PAGE_SIZE=10&XEMPENO=%s&XTOKEN=%s'\
             % (job_num, HIKE_ID, job_num, token)
    proxies = {'https': 'http://127.0.0.1:8888'}
    html = requests.get(url)
    #html = requests.get(url, proxies=proxies, verify=False)

    if html.status_code == 200:
        json_text = json.loads(html.text)
        if not json_text is None and len(json_text) > 0:
            return json_text[0]['TASK_ID']
                    
# 获取答案
def get_answers(date="2023-01-15", job_num='041538'):
    url = 'https://mcrm.citics.com/union/mobile/life/sport/hike/v2/selectQuestion?EMPE_NO=%s&HIKE_DATE=%s' % (job_num, date)
    proxies = {'https': 'http://127.0.0.1:8888'}
    html = requests.get(url)
    #html = requests.get(url, proxies=proxies, verify=False)

    if html.status_code == 200:
        json_text = json.loads(html.text)
        if not json_text is None and 'OPTION_LIST' in json_text:
            json_opt_list = json_text['OPTION_LIST']
            for opt in json_opt_list:
                if opt['IS_RIGHT'] == 'Y':
                    return opt, json_text['QUESTION']['SCORE']
            
        
# 回答问题
def answer_ques(task_id, opt,score, date="2023-01-15", job_num='041538'):
    url = 'https://mcrm.citics.com/union/mobile/life/sport/hike/v2/saveAnswer'
    proxies = {'https': 'http://127.0.0.1:8888'}
    header = {'Content-Type': 'application/x-www-form-urlencoded',
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'}
    opt['EMPE_NO'] = job_num
    opt['HIKE_ID'] = HIKE_ID
    opt['TASK_ID'] = task_id
    opt['SCORE'] = score
    opt['HIKE_DATE'] = date


    html = requests.post(url, data=opt, headers=header)
    #html = requests.post(url, data=opt, headers=header,proxies=proxies, verify=False)
    if html.status_code == 200:
        print('回答问题：', html.text)

if __name__ == '__main__':
    # 配置参数 BEGIN
    job_num = '041538'
    day_num = 1
    date_str = None
    #date_str = '2023-01-29'
    token='mobsIG5%2BE8m67B8Ao7IOSv%2FbcNuzjgUiZfSh1wWi2%2FIqNOBZtDITrgc4lq8M%2F0RtpWY2gmKW5V2K%2F7kRv7EadyuLJ70U266YnZ1djlOV0ND%2FnzstMNUhUsKGiaKzoneW6dryx9hgqaVyf9pMp7B1tCnjF%2FQjGAxWJUfcRr0zJVI%3D'
    # 配置参数 END

    task_id = get_last_task_id(token, job_num)
    print('待完成任务: ', task_id)
    date = get_date(date_str)
    for i in range(0, day_num): 
        date_str = date.strftime('%Y-%m-%d')
        print(date_str)
        opt, score = get_answers(date_str, job_num)
        print('获取答案：', opt)
        answer_ques(task_id, opt, score, date_str, job_num)
        answer_ques(task_id, opt, score, date_str, job_num)
        update_step(task_id, date_str, job_num)
        answer_ques(task_id, opt, score, date_str, job_num)
        date = before_date(date)